package com.eric.controller;

import cn.dev33.satoken.util.SaResult;
import com.eric.domain.dto.PurchaseRequisitionDTO;
import com.eric.domain.po.PurchaseRequisition;
import com.eric.service.PurchaseRequisitionService;
import com.mybatisflex.core.paginate.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  控制层。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/purchaseRequisition")
public class PurchaseRequisitionController {


    private final PurchaseRequisitionService purchaseRequisitionService;

    /**
     * 添加。
     *
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @Transactional
    public SaResult saveOnePurchaseRequisition(@Validated @RequestBody PurchaseRequisitionDTO purchaseRequisitionDTO) {
        return purchaseRequisitionService.saveOnePurchaseRequisition(purchaseRequisitionDTO);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @Transactional
    public SaResult remove(@PathVariable Long id) {
        return SaResult.data(purchaseRequisitionService.removeById(id));
    }

    /**
     * 根据主键更新。
     *
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Transactional
    public SaResult updateOne(@RequestBody PurchaseRequisitionDTO purchaseRequisitionDTO) {
        return purchaseRequisitionService.updateOne(purchaseRequisitionDTO);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("getListByPage")
    public SaResult getListByPage(Integer current,Integer pageSize) {
        return purchaseRequisitionService.getListByPageService(current,pageSize);
    }

    @GetMapping("getListCounts")

    public SaResult getListCounts(){
        return purchaseRequisitionService.getListCountsService();
    }
    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public PurchaseRequisition getInfo(@PathVariable Long id) {
        return purchaseRequisitionService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<PurchaseRequisition> page(Page<PurchaseRequisition> page) {
        return purchaseRequisitionService.page(page);
    }

}
